package com.person.ntv.controller.sys;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.Page;
import com.person.common.PageParams;
import com.person.exception.ServiceException;
import com.person.ntv.entity.sys.SysRole;
import com.person.ntv.service.sys.CommonService;
import com.person.ntv.service.sys.SysRoleService;

@Controller
@RequestMapping("/system/sysRole")
public class SysRoleController {
	@Autowired
		private SysRoleService  sysRoleService ;
	@Autowired
		private CommonService commonService;
		 
		@RequestMapping("/listPage")
		public String  getRolePage(ModelMap mv)throws Exception{
			initCode(mv);
			return "/system/role/roleList";
		}
		
		@RequestMapping("/roleList")
		@ResponseBody
		public Map<String,Object> getRoleList(PageParams myPage) throws Exception{
			Map<String,Object> map=new HashMap<String,Object>();
			Page<SysRole> page=sysRoleService.getRolePage(myPage);
	        map.put("total",page.getTotal());
	        map.put("rows",page.getResult());
			return map;
		}
		
		@RequestMapping("/addPage")
		public String  addRolePage(ModelMap mv) throws Exception{
			initCode(mv);
			return "/system/role/roleAdd";
		}
		
		@RequestMapping("/editPage")
		public String  editRolePage(String id,ModelMap mv) throws Exception{
			try {
		    SysRole role =sysRoleService.selectByPrimaryKey(new BigDecimal(id));
		    initCode(mv);
		    mv.addAttribute("role",role);
			} catch (ServiceException e) {
				mv.addAttribute("error", "1");
				mv.addAttribute("msg",e.getMessage()+"查询出错！");
			}
			return "/system/role/roleEdit";
		}
		
		@RequestMapping("/addRole")
		public String  addRole(ModelMap mv,SysRole sysRole) throws Exception{
			try {
				sysRoleService.insertSelective(sysRole);
				mv.addAttribute("error", "0");
				mv.addAttribute("msg","保存成功！");
			} catch (ServiceException e) {
				mv.addAttribute("error", "1");
				mv.addAttribute("msg",e.getMessage()+"保存失败！");
			}
			return "/system/role/roleAdd";
		}
		
		@RequestMapping("/editRole")
		public String  editRole(ModelMap mv,SysRole sysRole) throws Exception{
			try {
				sysRoleService.updateByPrimaryKey(sysRole); 
				mv.addAttribute("error", "0");
				mv.addAttribute("msg","保存成功！");
			} catch (ServiceException e) {
				mv.addAttribute("error", "1");
				mv.addAttribute("msg",e.getMessage()+"保存失败！");
			}
			return "/system/role/roleEdit";
		}
		
		@RequestMapping("/delRole")
		@ResponseBody
		public Map<String,Object>  delRole(BigDecimal[]ids,HttpServletRequest request) throws Exception{
			Map<String,Object> map=new HashMap<String,Object>();
			try {
				sysRoleService.deleteByPrimaryKeys(ids); 
				map.put("error", "0");
				map.put("msg","删除成功！");
			} catch (ServiceException e) {
				map.put("error", "1");
				map.put("msg",e.getMessage()+"删除失败！");
			}
			return map;
		}
		
		@RequestMapping("/detailPage")
		public String  detailRoleSetPage(String id,ModelMap mv) throws Exception{
			try {
		    SysRole role = sysRoleService.selectByPrimaryKey(new BigDecimal(id));
		    mv.addAttribute("role",role);
			} catch (ServiceException e) {
				mv.addAttribute("error", "1");
				mv.addAttribute("msg",e.getMessage()+"查询出错！");
			}
			return "/system/role/roleDetail";
		}
		
		@RequestMapping("/validateRoleName")
		@ResponseBody
		public boolean validateRoleName(SysRole sysRole) throws Exception{
			try {
				if(sysRole.getRoleName()==null)sysRole.setRoleName("");
				int cnt=commonService.validateOne("sys_role","role_name",sysRole.getRoleName(), sysRole.getId()==null?null:String.valueOf(sysRole.getId()));
				if(cnt==0)
				return true;
			} catch (ServiceException e) {
	           e.printStackTrace();
			}
			return false;
		}
		
		@RequestMapping("/validateRoleCode")
		@ResponseBody
		public boolean validateRoleCode(SysRole  sysRole) throws Exception{
			try {
				if(sysRole.getRoleCode()==null)sysRole.setRoleCode("");
				int cnt=commonService.validateOne("sys_role","role_code",sysRole.getRoleCode(), sysRole.getId()==null?null:String.valueOf(sysRole.getId())); 
				if(cnt==0)
				return true;
			} catch (ServiceException e) {
				e.printStackTrace();
			}
			return false;
		}
		
		private void initCode(ModelMap mv){
			mv.addAttribute("SysRoleStatus",JSONArray.fromObject(commonService.getCodeList("SysRoleStatus",true)).toString());
		}
}
